/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN 
 */
package ws.y10k.goloim.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import ws.y10k.goloim.util.HibernateUtil;

/**
 * @author natx
 *
 */
public class Boxes {
	
	HashSet<Box> hsBoxes = new HashSet<Box>();
	
	public Iterator<Box> iterator() {
		return hsBoxes.iterator();
	}
	
	public void add(Box newBox) throws BoxAlreadyExistsException {
		if ( this.boxExists(newBox) ) throw new BoxAlreadyExistsException();
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.save(newBox);
			transaction.commit();
			hsBoxes.add(newBox);
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
	}
	
	public void add(double width, double height, double depth, double weight) throws BoxAlreadyExistsException {
		Box newBox = new Box(width, height, depth, weight);
		this.add(newBox);
	}
	
	public Box getBox(double width, double height, double depth, double weight) throws BoxDoesNotExistException {
		
		boolean found = false;
		Box searchingBox = new Box(width, height, depth, weight);
		Box existingBox = null;
		Iterator<Box> itBoxes = hsBoxes.iterator();
		while ( itBoxes.hasNext() && !found ) {
			existingBox = itBoxes.next();
			if ( searchingBox.equals(existingBox) ) found = true;
		}
		if ( found ) return existingBox;
		else throw new BoxDoesNotExistException();
	}
	
	public boolean boxExists(double width, double height, double depth, double weight) {	

		boolean found = false;	
		Box box = new Box(width, height, depth, weight);
		Box existingBox = null;
		Iterator<Box> itBoxes = hsBoxes.iterator();
		while ( itBoxes.hasNext() && !found ) {
			existingBox = itBoxes.next();
			if ( box.equals(existingBox) ) found = true;
		}
		
		return found;
	}
	
	public boolean boxExists(Box box) {	

		boolean found = false;
		Box existingBox = null;
		Iterator<Box> itBoxes = hsBoxes.iterator();
		while ( itBoxes.hasNext() && !found ) {
			existingBox = itBoxes.next();
			if ( box.equals(existingBox) ) found = true;
		}
		
		return found;
	}

	public String[] getStringArray() {
		ArrayList<String> alBoxes = new ArrayList<String>();

		Iterator<Box> itBoxes = hsBoxes.iterator();
		while ( itBoxes.hasNext() ) {
			Box box = itBoxes.next();
			alBoxes.add(box.getWidth() + "x" + box.getHeight() + "x" + box.getDepth() + " " + box.getWeight());
		}
		String[] a = new String[alBoxes.size()];
		alBoxes.toArray(a);
		return a;
	}

	@SuppressWarnings("unchecked")
	public void load() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			List<Box> boxes = session.createQuery("from Box").list();
			for ( Iterator<Box> itBoxes = boxes.iterator(); itBoxes.hasNext(); ) {
				Box box = itBoxes.next();
				hsBoxes.add(box);
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

}
